package com.haoze.nurse.controller;

import com.alibaba.fastjson.JSONObject;
import com.haoze.common.response.Result;
import com.haoze.common.response.ResultGenerator;
import com.haoze.nurse.enumeration.NurseEnum;
import com.haoze.nurse.service.EmrNurseStatisticsService;
import com.haoze.nurse.service.FileIndexService;
import com.haoze.nurse.vo.DoctorAdvicesVO;
import com.haoze.nurse.vo.EmrFile;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping(value = "/nurse/fileindex")
public class FileIndexController {

    private static final Logger logger = LoggerFactory.getLogger(FileIndexController.class);
    @Resource
    FileIndexService fileIndexService;

    @Resource
    EmrNurseStatisticsService emrNurseStatisticsService;

    /**
     * 新增病历
     * @param tplId
     * @param inpatNum
     * @param deptId
     * @param reqNum
     * @param serialNo
     * @return
     */
    @GetMapping("/create")
    @ApiOperation(value = "新增病历")
    public Result createFileIndex(@RequestParam(required = false) final  String tplId,
                                  @RequestParam final String inpatNum,
                                  @RequestParam final String deptId,
                                  @RequestParam(required = false) final String reqNum,
                                  @RequestParam(required = false) final String serialNo){
        return fileIndexService.createFileIndex(tplId, inpatNum, deptId, reqNum, serialNo);
    }

    /**
     * 保存病历
     * @param json
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存病历")
    public Result saveFileIndex(@RequestBody final String json) {
        long t1 = System.currentTimeMillis();
        EmrFile emrFile = JSONObject.parseObject(json, EmrFile.class);
        long t2 = System.currentTimeMillis();
        logger.info("{} 解析耗时：{} ms", emrFile.getEmrFileIndexEntityOld().getEmrFileName(), t2 - t1);
        t1 = System.currentTimeMillis();
        Result result = fileIndexService.saveFileIndex(emrFile);
        t2 = System.currentTimeMillis();
        logger.info("{} 保存总耗时：{} ms", emrFile.getEmrFileIndexEntityOld().getEmrFileName(), t2 - t1);
        return result;
    }

    /**
     * 获取病历
     * @param emrFileId
     * @return
     */
    @GetMapping("/{emrFileId}")
    @ApiOperation(value = "获取病历")
    public Result getFileIndex(@PathVariable final String emrFileId) {
        EmrFile emrFile = fileIndexService.getFileIndex(emrFileId, NurseEnum.SHOW_INOUT_NO.getIndex());
        if (emrFile == null) {
            return ResultGenerator.genFailedResult("病历不存在或从未保存。");
        }
        return ResultGenerator.genOkResult(emrFile);
    }

    /**
     * 删除病历
     * @param emrFileId
     * @return
     */
    @DeleteMapping("/{emrFileId}")
    @ApiOperation(value = "删除病历")
    public Result deleteFileIndex(@PathVariable final String emrFileId) {
        return fileIndexService.deleteFileIndex(emrFileId);
    }

    /**
     * 恢复病历
     * @param emrFileId
     * @return
     */
    @GetMapping("recovery/{emrFileId}")
    @ApiOperation(value = "恢复病历")
    public Result recoveryFileIndex(@PathVariable final String emrFileId) {
        return fileIndexService.recoveryFileIndex(emrFileId);
    }

    /**
     * 获取病历修改痕迹
     * @param emrFileId
     * @return
     */
    @GetMapping("/change/{emrFileId}")
    public Result getChanges(@PathVariable final String emrFileId) {
        return fileIndexService.getChanges(emrFileId);
    }

    /**
     * 计算出入量返回标记节点
     * @param type
     * @param recordTime
     * @param inpatNum
     * @return
     */
    @GetMapping("/doStatistics")
    @ApiOperation(value = "计算出入量统计")
    public Result doStatistics(@RequestParam final String type,
                               @RequestParam final String recordTime,
                               @RequestParam final String inpatNum) {
        return emrNurseStatisticsService.doStatistics(type, recordTime, inpatNum);
    }


    @GetMapping("/getListHead")
    @ApiOperation(value = "获取列表表头")
    public Result getHeadDetail (@RequestParam final String tplId) {
        return fileIndexService.getListHead(tplId);
    }

    @GetMapping("/detailList")
    @ApiOperation(value = "获取记录列表")
    public Result getDetailList(@RequestParam final String tplId, @RequestParam final String inpatNum,@RequestParam final String strTm,
                          @RequestParam final String endTm,@RequestParam final Integer page,@RequestParam final Integer pageSize,@RequestParam(defaultValue = "1",required = false) final String isDesc) {
        return fileIndexService.getDetailList(tplId, inpatNum, strTm, endTm, page, pageSize,isDesc);
    }


    @GetMapping("/getNurseFileTree")
    @ApiOperation(value = "获取左侧列表")
    public Result getNurseFileTree(@RequestParam final String inpatNum) {
        return fileIndexService.getNurseFileTree(inpatNum);
    }

    @GetMapping("/getInOutSummary")
    @ApiOperation(value = "获取出入量总结")
    public Result getInOutSummary(@RequestParam final String inpatNum,
                                  @RequestParam final String startTime,
                                  @RequestParam final String endTime){
        return emrNurseStatisticsService.getInOutSummary(inpatNum,startTime,endTime);
    }

    @GetMapping("/getTplDetail")
    @ApiOperation(value = "获取模板详情")
    public Result getTplDetail(@RequestParam final String tplDetailId,@RequestParam final String tplId) {
        return fileIndexService.getTplDetail(tplDetailId,tplId);
    }

    @GetMapping("/getRealize")
    @ApiOperation(value = "获取意识")
    public Result getRealize(@RequestParam final String inpatNum,@RequestParam final String tplId) {
        return fileIndexService.getRealize(inpatNum,tplId);
    }

    @GetMapping("/getEmrFileId")
    @ApiOperation(value = "获取病案首页护理附页文书ID")
    public Result getEmrFileId(@RequestParam final String inpatNum){
        return fileIndexService.getEmrFileId(inpatNum);
    }
}
